package com.quectel.aiot.controller;

import com.quectel.aiot.entity.Product;
import com.quectel.aiot.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/products")
@Api(tags = "产品管理", description = "产品管理相关接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/sync/{projectId}")
    @ApiOperation("同步指定项目下的产品")
    public String syncProducts(
            @ApiParam(name = "projectId", value = "项目ID", required = true)
            @PathVariable String projectId) {
        try {
            productService.syncProducts(projectId);
            return "产品同步成功";
        } catch (Exception e) {
            log.error("产品同步失败", e);
            return "产品同步失败: " + e.getMessage();
        }
    }

    @PostMapping("/sync/all")
    @ApiOperation("同步所有项目下的产品")
    public String syncAllProjectsProducts() {
        try {
            productService.syncAllProjectsProducts();
            return "所有项目产品同步成功";
        } catch (Exception e) {
            log.error("所有项目产品同步失败", e);
            return "所有项目产品同步失败: " + e.getMessage();
        }
    }

    @GetMapping
    @ApiOperation("获取所有产品列表")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }
} 